checkLogin();

scorllInit();

var pay_return = GetQueryString('pay_return');

if (pay_return) {
    clearCart();
}

//所有订单请求
var type = 1;
fenye_options.url = order_index;
fenye_options.dataName = 'order_list';
fenye_options.params = {type: type};
fenye_options.tmpImports = {tabType: type};
var shop_mobile = '';


fenye_options.callback = function (res) {
    console.log(res)
    shop_mobile = res.data.contact || '';
    //如果没有数据
    var info = res.data.order_list.data;
    if(isEmpty(info) || info.length <=0){
        $('.bac_gray').removeClass('mui-table-view');
        //如果没有数据 显示 无数据
        $('#fenye_list').html("");
        var html = '<div class="no_data_head"><img class="no_data_img" src="image/dingdan.png">' +
            '<p class="no_data_p1">您暂时没有相关订单</p><p>去商城看看？</p>' +
            '<button class="mui-btn no_data_button">立即去商城</button></div>'
        $('#fenye_list').append(html);
    }

    //点击立即去商城
    $('.no_data_button').on('tap',function () {
        redirect('index.html');
    })

//先解绑事件，防止上拉加载造成二次绑定，导致事件被执行多次
    $(".toDetail").unbind('click');
    //绑定点击事件，添加购物车
    $(".toDetail").on('tap', function () {

        var order_id = parseInt(this.getAttribute('data'));
        redirect("order-detail.html?id=" + order_id);

    });

    var type = type;

    //获取水店信息
    var shop = getCache(login_key);
    $('.shop-name').text(shop.shop_name);


    $('.reminder').on('tap', function () {

        var mobile = parseInt(this.getAttribute('data-mobile'));
        //弹框
        var btnArray = ['取消', '确定'];
        mui.confirm('拨打' + mobile + '?', btnArray, function (e) {
            if (e.index == 1) {
                mui.plusReady(function () {
                    // 在这里调用plus api
                    plus.device.dial(mobile, true);
                });


            }
        })
    })

    //點擊付取消訂單按鈕
    $('.cancel_order').bind('click', function () {
        var id = parseInt(this.getAttribute('data-id')) || '';
        if (id == undefined || id == null || id == '' || shop_mobile == '') {
            return;
        }
        mui.confirm('取消订单需联系商家，联系方式：'+shop_mobile, '提示', ['联系商家', '取消'], function (e) {
            if (e.index == 0) { //如果確認刪除
                window.location.href = "tel:" + shop_mobile;//data-mobile;
            }
        })
    })

    $('.cuidan').bind('click', function () {
        var tel = parseInt(this.getAttribute('data'));
        window.location.href = 'tel:'+tel;
    })


    //點擊付款按鈕
    $('.order-pay').bind('click', function () {
        //获取id
        var order_id = parseInt(this.getAttribute('data-id'));
        if (order_id == undefined || order_id == null || order_id === '') {
            return;
        }
        mui.confirm("确认支付？", "提示", ['取消', '确认'], function (e) {
            if (e.index == 1) {
                var success = function (res) {
                    payment(res.data);
                };
                postData(order_pay, combineParams({order_id: order_id}), success);
            } else {
                redirect('fail.html?type=1');
            }
        })
    })
};


//微信支付
function payment(data) {
    if (window.__wxjs_environment === 'miniprogram') {
        data.show_type = 1;
        var status = false;
        wx.miniProgram.navigateTo({
            url: '/pages/qds/wxpay/index?params=' + escape(JSON.stringify(data))
        })
        var mask = mui.createMask(function () {
            return false;
        });
        mask.show();//显示遮罩
        mui.confirm('', '是否支付成功 ?', ['是', '否'], function (e) {
            if (e.index == 0) {
                var success = function (res) {
                    if (res.data.result != undefined) {
                        status = res.data.result;
                    }
                    if (status) {
                        clearCart();
                        redirect('success.html?type=1');
                    } else {
                        redirect('fail.html?type=1');
                    }
                };
                getData(order_check_status, combineParams({id: data.id}), success);
            } else {
                redirect('fail.html?type=1');
            }
        })

    } else {
        wxPay(data,1);
    }
}


//选项卡切换下拉加载
mui('#segmentedControl').on('tap', 'a', function (e) {
    window.top.location.href = this.href;
    type = parseInt(this.getAttribute('data'));
    page = 1;
    is_more = true;
    fenye_options.params = {type: type}
    fenye_options.tmpImports = {tabType: type};
    $('#fenye_list').empty();
    mui("#scroll").pullRefresh().enablePullupToRefresh(); //启用上拉加载控件
    mui('#scroll').pullRefresh().refresh(true); //重置上拉
    mui('#scroll').pullRefresh().endPullupToRefresh(false);//开启加载
    mui('#scroll').pullRefresh().scrollTo(0, 0, 100); //滚动到顶部
});

function scorllInit() {
    var info = getScreen();
    var header_height = 0; //顶部标题
    if (is_local || is_weixn()) {
        header_height = parseInt($(".mui-bar").height());
    }
    if(isNaN(header_height)){
        header_height = 0;
    }
    var category_height = parseInt($("#segmentedControl").height()); //固定选项卡
    var scroll_height = info[1] - header_height - category_height + 'px';
    fenye_options.scrollHeight = scroll_height; //设置分页滚轴高度（自适应）
    mui('.mui-control-content').each(function () {
        this.style.height = scroll_height;
    });
    mui('.mui-scroll-wrapper').each(function (k, v) {
        mui(v).scroll({
            deceleration: (mui.os.ios ? 0.003 : 0.0006),
            indicators: false  //隐藏一条滚动条 增大减速系数。。。
        });
    });
}

